/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.servlet.admin.restaurant;

import com.supinfo.youfood.entity.Restaurant;
import com.supinfo.youfood.service.RestaurantService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 *
 * @author TaKeO
 */
@WebServlet(name = "AddRestoServlet", urlPatterns = {"/admin/ajouter-restaurant"})
public class AddRestoServlet extends HttpServlet {

    @EJB
    private RestaurantService restoService;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {   
        if(request.getParameter("id") != null)
        {
            Restaurant resto = restoService.findRestoById(new Long(request.getParameter("id")));
            request.setAttribute("resto", resto);
        }    
        
        request.getRequestDispatcher("/jsp/admin/ajouter-restaurant.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        Restaurant resto;
        if(request.getParameter("id") != null)
        {
            resto = restoService.findRestoById(new Long(request.getParameter("id")));
        }
        else
        {
            resto = new Restaurant();
        }
        
        resto.setNom(request.getParameter("nom"));
        resto.setAdresse(request.getParameter("adresse"));
        resto.setMail(request.getParameter("mail"));
        resto.setTelephone(request.getParameter("tel"));
        
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        Set<ConstraintViolation<Restaurant>> constraintViolations = validator.validate(resto);

        if (constraintViolations.size() > 0) 
        {
            List<String> errors = new ArrayList<String>();
            
            for (ConstraintViolation<Restaurant> contraintes : constraintViolations) 
            {
                errors.add(contraintes.getPropertyPath()+" "+contraintes.getMessage());
            }
            
            request.setAttribute("errors", errors);
            getServletContext().getRequestDispatcher("/jsp/admin/ajouter-restaurant.jsp").forward(request, response);
        }
        else
        {
            if(request.getParameter("id") != null)
            {
                restoService.refreshResto(resto);
            }
            else
            {
                restoService.addResto(resto);
            }      
        
            response.sendRedirect("/YouFood/admin/liste-restaurants");
        }
    }

}
